﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace YArchitech.HVAC.PipeSetting
{
	public class SystemTypeXmlHelper
	{
		public XmlDocument XmlDoc
		{
			get
			{
				return this.xmlDoc;
			}
			set
			{
				this.xmlDoc = value;
			}
		}

		public SystemTypeXmlHelper()
		{
			this.xmlDoc = new XmlDocument();
		}

		public SystemTypeXmlHelper(string filePath)
		{
			this.filePath = filePath;
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(filePath);
		}

		public void Clear()
		{
			this.xmlDoc.DocumentElement.RemoveAll();
		}

		private XmlDocument GetSingleXmlDoc()
		{
			if (this.xmlDoc == null)
			{
				if (!File.Exists(this.filePath))
				{
					return null;
				}
				this.xmlDoc = new XmlDocument();
				this.xmlDoc.Load(this.filePath);
			}
			return this.xmlDoc;
		}

		public string GetRootName()
		{
			return this.xmlDoc.DocumentElement.Name;
		}

		public List<SystemTypeInfo> GetPipingSystemType(string xpath)
		{
			List<SystemTypeInfo> list = new List<SystemTypeInfo>();
			IList<string> pipeTypeNames = PipeSettingManager.GetPipeTypeNames();
			foreach (object obj in this.xmlDoc.DocumentElement.SelectNodes(xpath))
			{
				XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
				SystemTypeInfo systemTypeInfo = new SystemTypeInfo();
				systemTypeInfo.SysType = xmlElement.GetAttribute("pipingSystemType");
				systemTypeInfo.Abbreviation = xmlElement.GetAttribute("pipingSystemTypeAbb");
				systemTypeInfo.PipeType = xmlElement.GetAttribute("pipeType");
				systemTypeInfo.LineColor = xmlElement.GetAttribute("lineColor");
				systemTypeInfo.LinePattern = xmlElement.GetAttribute("linePattern");
				systemTypeInfo.LineWeight = xmlElement.GetAttribute("lineWeight");
				systemTypeInfo.SysCategory = xmlElement.GetAttribute("pipingClassify");
				systemTypeInfo.GroupName = xmlElement.GetAttribute("group");
				if (string.IsNullOrEmpty(systemTypeInfo.PipeType) || !pipeTypeNames.Contains(systemTypeInfo.PipeType))
				{
					systemTypeInfo.PipeType = ((pipeTypeNames.Count > 0) ? pipeTypeNames[0] : systemTypeInfo.PipeType);
				}
				list.Add(systemTypeInfo);
			}
			return list;
		}

		public IDictionary<string, IList<SystemTypeInfo>> GetPipingTypeWithGroup(string xpath)
		{
			IDictionary<string, IList<SystemTypeInfo>> result;
			try
			{
				string text = string.Empty;
				IDictionary<string, IList<SystemTypeInfo>> dictionary = new Dictionary<string, IList<SystemTypeInfo>>();
				IList<string> pipeTypeNames = PipeSettingManager.GetPipeTypeNames();
				IList<string> linePatternName = PipeSettingManager.GetLinePatternName();
				IList<string> lineWeight = PipeSettingManager.GetLineWeight();
				IList<string> pipingClassify = PipeSettingManager.PipingClassify;
				foreach (object obj in this.xmlDoc.DocumentElement.SelectNodes(xpath))
				{
					XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
					text = xmlElement.GetAttribute("name");
					if (string.IsNullOrEmpty(text))
					{
						throw new Exception();
					}
					IList<SystemTypeInfo> list = new List<SystemTypeInfo>();
					foreach (object obj2 in xmlElement.ChildNodes)
					{
						XmlElement xmlElement2 = ((XmlNode)obj2) as XmlElement;
						SystemTypeInfo systemTypeInfo = new SystemTypeInfo();
						systemTypeInfo.SysType = xmlElement2.GetAttribute("pipingSystemType");
						systemTypeInfo.Abbreviation = xmlElement2.GetAttribute("pipingSystemTypeAbb");
						systemTypeInfo.PipeType = xmlElement2.GetAttribute("pipeType");
						systemTypeInfo.LineColor = xmlElement2.GetAttribute("lineColor");
						systemTypeInfo.LinePattern = xmlElement2.GetAttribute("linePattern");
						systemTypeInfo.LineWeight = xmlElement2.GetAttribute("lineWeight");
						systemTypeInfo.SysCategory = xmlElement2.GetAttribute("pipingClassify");
						systemTypeInfo.GroupName = text;
						if (string.IsNullOrEmpty(systemTypeInfo.SysType))
						{
							throw new Exception();
						}
						if (string.IsNullOrEmpty(systemTypeInfo.PipeType) || !pipeTypeNames.Contains(systemTypeInfo.PipeType))
						{
							systemTypeInfo.PipeType = ((pipeTypeNames.Count > 0) ? pipeTypeNames[0] : systemTypeInfo.PipeType);
						}
						if (string.IsNullOrEmpty(systemTypeInfo.LineColor))
						{
							systemTypeInfo.LineColor = "默认";
						}
						if (string.IsNullOrEmpty(systemTypeInfo.LinePattern))
						{
							systemTypeInfo.LinePattern = "默认";
						}
						else if (!linePatternName.Contains(systemTypeInfo.LinePattern))
						{
							systemTypeInfo.LinePattern = "默认";
						}
						if (string.IsNullOrEmpty(systemTypeInfo.LineWeight))
						{
							systemTypeInfo.LineWeight = "默认";
						}
						else if (!lineWeight.Contains(systemTypeInfo.LineWeight))
						{
							systemTypeInfo.LineWeight = "默认";
						}
						if (string.IsNullOrEmpty(systemTypeInfo.SysCategory))
						{
							systemTypeInfo.SysCategory = "其他";
						}
						else if (!pipingClassify.Contains(systemTypeInfo.SysCategory))
						{
							systemTypeInfo.SysCategory = "其他";
						}
						list.Add(systemTypeInfo);
					}
					dictionary.Add(text, list);
				}
				result = dictionary;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public IList<string> GetStandardStyle()
		{
			string text = string.Empty;
			IList<string> list = new List<string>();
			foreach (object obj in this.xmlDoc.DocumentElement.ChildNodes)
			{
				text = (((XmlNode)obj) as XmlElement).GetAttribute("name");
				if (!string.IsNullOrEmpty(text))
				{
					list.Add(text);
				}
			}
			return list;
		}

		public bool IsExist(string xpath)
		{
			new List<string>();
			return this.xmlDoc.DocumentElement.SelectSingleNode(xpath) != null;
		}

		public XmlElement CreateNode(string nodeName)
		{
			if (string.IsNullOrEmpty(nodeName))
			{
				return null;
			}
			return this.xmlDoc.CreateElement(nodeName);
		}

		public XmlElement CreateNode(string nodeName, Hashtable hashtable)
		{
			if (string.IsNullOrEmpty(nodeName))
			{
				return null;
			}
			XmlElement xmlElement = this.xmlDoc.CreateElement(nodeName);
			foreach (object obj in hashtable)
			{
				DictionaryEntry dictionaryEntry = (DictionaryEntry)obj;
				xmlElement.SetAttribute(dictionaryEntry.Key.ToString(), dictionaryEntry.Value.ToString());
			}
			return xmlElement;
		}

		public XmlDeclaration CreateDeclaration()
		{
			if (this.xmlDoc == null)
			{
				this.xmlDoc = new XmlDocument();
			}
			return this.xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
		}

		public void Save()
		{
			this.xmlDoc.Save(this.filePath);
		}

		public void Save(string fileName)
		{
			this.xmlDoc.Save(fileName);
		}

		private string filePath;

		private XmlDocument xmlDoc;
	}
}
